package cn.wolfcode.book.servlet;

import cn.wolfcode.book.entity.Book;
import cn.wolfcode.book.service.BookService;
import cn.wolfcode.book.service.impl.BookServiceImpl;
import cn.wolfcode.entity.BookQueryObject;
import cn.wolfcode.entity.PageResult;
import cn.wolfcode.util.IsNotNullUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * @author 李新雨的项目86176
 * @version 1.0
 * @date 2024-10-28 12:56
 */
//// TODO: 2024/10/28  提升优先级
@WebServlet("/book")
public class BookServlet extends HttpServlet {
    private final BookService bookService=new BookServiceImpl();


    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        String userRequest=req.getParameter("userRequest");
        userRequest=(userRequest==null)?"":userRequest;
        switch (userRequest){
            case "del":
                delete(req,resp);
                break;
            case "input":
                input(req,resp);
                break;
            case "save":
                save(req,resp);
                break;
            default:
                showAll(req,resp);
        }


    }

    private void showAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        // TODO: 2024/10/30 利用共享session解决问题
        // TODO: 2024/11/7  登陆校验----利用过滤器
//        req.getRequestDispatcher("/randomCode").forward(req,resp);
//        Object user_in_session = req.getSession().getAttribute("USER_IN_SESSION");
//
////        User login = LoginUtils.isLogin(req, resp);
//        if(user_in_session==null){
//            resp.sendRedirect("/login.jsp");
//            return;
//        }
//        QueryObject queryObject=new QueryObject();
          BookQueryObject queryObject=new BookQueryObject();



        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");

        String keyword = req.getParameter("keyword");
        String maxSalePrice = req.getParameter("maxSalePrice");
        String minSalePrice = req.getParameter("minSalePrice");
//        BigDecimal maxSalP = new BigDecimal(maxSalePrice);
//        BigDecimal minSalP = new BigDecimal(minSalePrice);

//        System.out.println(keyword+maxSalePrice);

        if(IsNotNullUtils.isNotNull(currentPage)) {
            queryObject.setCurrentPage(Integer.parseInt(currentPage));
        }
        if(IsNotNullUtils.isNotNull(pageSize)) {
            queryObject.setPageSize(Integer.parseInt(pageSize));
        }
        if(IsNotNullUtils.isNotNull(keyword)){
            queryObject.setKeyword(keyword);
        }
        if(IsNotNullUtils.isNotNull(maxSalePrice)){
            queryObject.setMaxSalePrice(new BigDecimal(maxSalePrice));
        }
        if(IsNotNullUtils.isNotNull(minSalePrice)){
            queryObject.setMinSalePrice(new BigDecimal(minSalePrice));
        }
        PageResult<Book> query = bookService.query(queryObject);
//        List<Book> books = bookService.selAll();
//        req.setAttribute("books",books);
        req.setAttribute("pageResult",query);
        req.setAttribute("bqo",queryObject);
//        req.setAttribute("qo",queryObject);
        req.getRequestDispatcher("/WEB-INF/views/book/list.jsp").forward(req,resp);
    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Book book= new Book();
        String id = req.getParameter("id");
        String bname = req.getParameter("bname");
        String author = req.getParameter("author");
        String salePrice = req.getParameter("salePrice");
        String costPrice = req.getParameter("costPrice");
        String classify = req.getParameter("classify");
        if(IsNotNullUtils.isNotNull(id)) {
            book.setId(Long.valueOf(id));
        }
        if(IsNotNullUtils.isNotNull(bname)) {
            book.setBname(bname);
        }
        if(IsNotNullUtils.isNotNull(author)) {
            book.setAuthor(author);
        }
        if(IsNotNullUtils.isNotNull(salePrice)) {
            book.setSalePrice(new BigDecimal(salePrice));
        }
        if(IsNotNullUtils.isNotNull(costPrice)) {
            book.setCostPrice(new BigDecimal(costPrice));
        }
        if(IsNotNullUtils.isNotNull(classify)) {
            book.setClassify(Long.valueOf(classify));
        }
        bookService.save(book);
        resp.sendRedirect("/book");
    }

    private void input(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        if(IsNotNullUtils.isNotNull(id)){

            Book book = bookService.selOneById(Long.valueOf(id));

            req.setAttribute("book",book);
        }
        req.getRequestDispatcher("/WEB-INF/views/book/addOrUpdate.jsp").forward(req,resp);
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id=req.getParameter("id");
        if(id!=null) {
            bookService.delById(Long.valueOf(id));
        }
        resp.sendRedirect("/book");

    }
}
